﻿using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.IO;

using Common.Model;
using Common.Error;
using OpenAPI.Model.API;
using OpenAPI.BLL.API;
using OpenAPI.Web.Mvc.Helper;
using OpenAPI.Web.Mvc.Filter;

namespace OpenAPI.Web.Mvc.Controllers
{
    [ExceptionFilter]
    [AuthFilter]
    public class DocumentController : Controller
    {
        [HttpGet]
        public ActionResult Index(string id)
        {
            string doc_cd = id;
            DocumentInfo doc = OpenAPI.BLL.API.DocumentManager.Get(doc_cd);

            return View(doc);
        }


        [HttpGet]
        public ActionResult Doc(string id)
        {
            string method_name = id;
            DocumentInfo doc = new DocumentInfo();

            MethodInfo method = OpenAPI.BLL.API.MethodManager.GetMethod(method_name);
            if (method != null)
            {
                IList<MethodUnSignArgumentInfo> unSignArguments = OpenAPI.BLL.API.MethodManager.GetUnSignArguments(method_name);
                IList<MethodSignArgumentInfo> signArguments = OpenAPI.BLL.API.MethodManager.GetSignArguments(method_name);
                doc = this.GetDoc(method_name, method, unSignArguments, signArguments);
            }

            return View(doc);
        }
        private DocumentInfo GetDoc(string method_name, MethodInfo method, IList<MethodUnSignArgumentInfo> unSignArguments, IList<MethodSignArgumentInfo> signArguments)
        {
            string template_path = Server.MapPath("/App_Data/Template/");
            string temp_filename = "API_Document.htm";
            string _Content = "";

            StreamReader _objReader = new StreamReader(template_path + temp_filename, System.Text.Encoding.UTF8);
            _Content = _objReader.ReadToEnd();
            _objReader.Close();

            #region un sign arguments

            int un_sign_begin = _Content.IndexOf("<@--WHILE_UN_SIGN_BEGIN-->");
            int un_sign_end = _Content.IndexOf("<@--WHILE_UN_SIGN_END-->");
            string un_sign_content = _Content.Substring(un_sign_begin, un_sign_end + "<@--WHILE_UN_SIGN_END-->".Length - un_sign_begin);

            string temp_un_sign_content = un_sign_content;
            temp_un_sign_content = temp_un_sign_content.Replace("<@--WHILE_UN_SIGN_BEGIN-->", "");
            temp_un_sign_content = temp_un_sign_content.Replace("<@--WHILE_UN_SIGN_END-->", "");
            string replace_un_sign_content = "";
            foreach (MethodUnSignArgumentInfo unSignArgument in unSignArguments)
            {
                string temp = temp_un_sign_content;
                temp = temp.Replace("<@--UN_SIGN_ARGUMENT_NAME-->", unSignArgument.MethodUnSignArgumentPK.ARGUMENT_NAME);
                temp = temp.Replace("<@--UN_SIGN_ARGUMENT_DATATYPE-->", unSignArgument.ARGUMENT_DATATYPE);
                temp = temp.Replace("<@--UN_SIGN_ARGUMENT_VALUE-->", unSignArgument.ARGUMENT_VALUE);
                temp = temp.Replace("<@--UN_SIGN_ARGUMENT_DESC-->", unSignArgument.ARGUMENT_DESC);
                replace_un_sign_content += temp;
            }

            #endregion


            #region sign arguments

            int sign_begin = _Content.IndexOf("<@--WHILE_SIGN_BEGIN-->");
            int sign_end = _Content.IndexOf("<@--WHILE_SIGN_END-->");
            string sign_content = _Content.Substring(sign_begin, sign_end + "<@--WHILE_SIGN_END-->".Length - sign_begin);

            string temp_sign_content = sign_content;
            temp_sign_content = temp_sign_content.Replace("<@--WHILE_SIGN_BEGIN-->", "");
            temp_sign_content = temp_sign_content.Replace("<@--WHILE_SIGN_END-->", "");
            string replace_sign_content = "";
            foreach (MethodSignArgumentInfo signArgument in signArguments)
            {
                string temp = temp_sign_content;
                temp = temp.Replace("<@--SIGN_ARGUMENT_NAME-->", signArgument.MethodSignArgumentPK.ARGUMENT_NAME);
                temp = temp.Replace("<@--SIGN_ARGUMENT_DATATYPE-->", signArgument.ARGUMENT_DATATYPE);
                temp = temp.Replace("<@--SIGN_ARGUMENT_VALUE-->", signArgument.ARGUMENT_VALUE);
                temp = temp.Replace("<@--SIGN_ARGUMENT_DESC-->", signArgument.ARGUMENT_DESC);
                replace_sign_content += temp;
            }

            //replace
            _Content = _Content.Replace("<@--METHOD_DESC-->", method.METHOD_DESC);
            _Content = _Content.Replace("<@--METHOD_NAME-->", method.METHOD_NAME);
            _Content = _Content.Replace("<@--METHOD_TEXT-->", method.METHOD_TEXT);
            _Content = _Content.Replace(un_sign_content, replace_un_sign_content);
            _Content = _Content.Replace(sign_content, replace_sign_content);
            _Content = _Content.Replace("<@--API_SECRET-->", APIHelper.GetSignSecretKey(method.METHOD_NAME));
            _Content = _Content.Replace("<@--METHOD_RETURN-->", method.METHOD_RETURN);

            #endregion

            DocumentInfo doc = new DocumentInfo();
            doc.DOC_CD = method.METHOD_NAME;
            doc.DOC_SUBJECT = method.METHOD_TEXT;
            doc.DOC_BODY = _Content;

            return doc;
        }


        #region Manage

        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [ManagerAuthFilter]
        [HttpPost]
        [ValidateInput(false)]
        [ActionName("Add")]
        public ActionResult AjaxAdd()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            DocumentInfo doc = new DocumentInfo();
            doc.DOC_CD = Utility.StringHelper.GetPost("doc_cd").Trim();
            doc.DOC_SUBJECT = Utility.StringHelper.GetPost("doc_subject").Trim();
            doc.DOC_AUTHOR = Utility.StringHelper.GetPost("doc_author").Trim();
            doc.DOC_DATETIME = DateTime.Now;
            doc.DOC_TAGS = Utility.StringHelper.GetPost("doc_tags").Trim();
            doc.DOC_BODY = Utility.StringHelper.GetPost("doc_body").Trim();

            if (string.IsNullOrEmpty(doc.DOC_CD))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Doc code can't empty.");
            }
            else if (string.IsNullOrEmpty(doc.DOC_SUBJECT))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Doc subject can't empty.");
            }
            else if (string.IsNullOrEmpty(doc.DOC_AUTHOR))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Doc author can't empty.");
            }
            else if (string.IsNullOrEmpty(doc.DOC_BODY))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Doc body can't empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.DocumentManager.Add(doc, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [HttpGet]
        public ActionResult Modify(string id)
        {
            string doc_cd = id;
            DocumentInfo doc = OpenAPI.BLL.API.DocumentManager.Get(doc_cd);

            return View(doc);
        }


        [ManagerAuthFilter]
        [HttpPost]
        [ValidateInput(false)]
        [ActionName("Modify")]
        public ActionResult AjaxModify()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            DocumentInfo doc = new DocumentInfo();
            doc.DOC_CD = Utility.StringHelper.GetPost("doc_cd").Trim();
            doc.DOC_SUBJECT = Utility.StringHelper.GetPost("doc_subject").Trim();
            doc.DOC_AUTHOR = Utility.StringHelper.GetPost("doc_author").Trim();
            doc.DOC_DATETIME = DateTime.Now;
            doc.DOC_TAGS = Utility.StringHelper.GetPost("doc_tags").Trim();
            doc.DOC_BODY = Utility.StringHelper.GetPost("doc_body").Trim();

            if (string.IsNullOrEmpty(doc.DOC_CD))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Doc code can't empty.");
            }
            else if (string.IsNullOrEmpty(doc.DOC_SUBJECT))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Doc subject can't empty.");
            }
            else if (string.IsNullOrEmpty(doc.DOC_AUTHOR))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Doc author can't empty.");
            }
            else if (string.IsNullOrEmpty(doc.DOC_BODY))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Doc body can't empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.DocumentManager.Modify(doc, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult List()
        {
            IList<DocumentInfo> docs = OpenAPI.BLL.API.DocumentManager.List();

            return View(docs);
        }


        [ManagerAuthFilter]
        [HttpPost]
        [ActionName("Delete")]
        public ActionResult AjaxDelete()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            string doc_cd = Utility.StringHelper.GetPost("doc_cd").Trim();

            if (string.IsNullOrEmpty(doc_cd))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Doc code can't empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.DocumentManager.Delete(doc_cd, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult Pop_DocView(string id)
        {
            string doc_cd = id;
            DocumentInfo doc = OpenAPI.BLL.API.DocumentManager.Get(doc_cd);

            return View(doc);
        }
        #endregion

    }
}
